﻿using Mapster;
using SqlSugar;
using Zhp.Common.WebApp;
using Zhp.Entity.Sys;
using Zhp.IService.Sys;
using Zhp.Model.Sys;
using Zhp.Service.Base;
using Zhp.SqlSugar.Repository;

namespace Zhp.Service.Sys
{
    public class SysRoleService : BaseService<SysRole>, ISysRoleService
    {

        public SysRoleService(ICurrentUser currentUser, IBaseRepository<SysRole> rep)
        {
            _currentUser = currentUser;
            _rep = rep;
        }

        public async Task<List<RoleModel>> GetRole(RoleQuery query)
        {
            var roleList = await _rep.GetPageListAsync(new List<IConditionalModel>
            {
            }, query, x => x.CreateTime, OrderByType.Desc);

            return roleList.Adapt<List<RoleModel>>();
        }

        public async Task<bool> Create(RoleCreate create)
        {
            var role = create.Adapt<SysRole>();

            return await _rep.InsertAsync(role);
        }

        public async Task<bool> Update(RoleCreate update)
        {
            var role = await _rep.GetByIdAsync(update.Id);

            if (role != null)
            {
                update.Adapt(role);

                return await _rep.UpdateAsync(role);
            }

            return false;
        }

        public async Task<bool> Delete(string id)
        {
            return await _rep.DeleteByIdAsync(id);
        }

        public async Task<SysRole> QueryByName(string name)
        {
            return await _rep.GetFirstAsync(x => x.RoleName == name);
        }
    }
}
